﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace CSharpDevTools.Core
{
    internal class Base64Helper
    {
        #region BitmapImage与byte[]互转
        /// <summary>
        /// BitmapImage转byte
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static byte[] BitmapImageToByteArray(BitmapImage bmp)
        {
            byte[] byteArray = null;
            try
            {
                Stream sMarket = bmp.StreamSource;
                if (sMarket != null && sMarket.Length > 0)
                {
                    //很重要，因为Position经常位于Stream的末尾，导致下面读取到的长度为0。 
                    sMarket.Position = 0;

                    using (BinaryReader br = new BinaryReader(sMarket))
                    {
                        byteArray = br.ReadBytes((int)sMarket.Length);
                    }
                }
            }
            catch
            {
                //other exception handling 
            }

            return byteArray;
        }

        /// <summary>
        /// byte[]转换为BitmapImage
        /// </summary>
        /// <param name="byteArray"></param>
        /// <returns></returns>
        public static BitmapImage ByteArrayToBitmapImage(byte[] byteArray)
        {
            BitmapImage bmp = null;
            try
            {
                bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.StreamSource = new MemoryStream(byteArray);
                bmp.EndInit();
            }
            catch
            {
                bmp = null;
            }

            return bmp;
        }
        #endregion

        /// <summary>
        /// 图片转为base64
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static string ToBase64(BitmapImage bmp)
        {
            try
            {
                return Convert.ToBase64String(BitmapImageToByteArray(bmp));
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 字符串转为base64
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ToBase64(string data)
        {
            return Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(data));
        }

        /// <summary>
        /// base64转为图片
        /// </summary>
        /// <param name="base64"></param>
        /// <returns></returns>
        public static BitmapImage FromBase64(string base64)
        {
            try
            {
                byte[] bytes = Convert.FromBase64String(base64);
                return ByteArrayToBitmapImage(bytes);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// base64解码
        /// </summary>
        /// <param name="base64"></param>
        /// <returns></returns>
        public static string FromBase64String(string base64)
        {
            return UTF8Encoding.UTF8.GetString(Convert.FromBase64String(base64));
        }
    }
}
